//
//  MedicalViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/3/7.
//  Copyright © 2016年 zhangchao. All rights reserved.
//)

#import "MedicalViewController.h"
#import <Masonry.h>

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
#define kViewHeight 40
#define kLabelHeight 30

@interface MedicalViewController ()<UITextViewDelegate>
@property (nonatomic,strong) UIScrollView *backGroudView;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *sexLabel;
@property (nonatomic,strong) UILabel *ageLabel;
/**
 *  主诉
 */
@property (nonatomic,strong) UITextView *chiefComplaintView;
/**
 *  现病史
 */
@property (nonatomic,strong) UITextView *presentHistoryView;
@property (nonatomic,strong) UILabel *hospitalNameLabel;
@property (nonatomic,strong) UILabel *doctorNameLabel;
/**
 *  挂号时间
 */
@property (nonatomic,strong) UILabel *medicalDateLabel;
@end

@implementation MedicalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电子病历";
    
    //个人病例布局
    [self addViews];
}

- (void)addViews
{
    self.backGroudView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.backGroudView.contentSize = CGSizeMake(kWidth , kHeight * 1.3);
    //底色
    self.backGroudView.backgroundColor = [UIColor brownColor];
    self.backGroudView.alpha = 0.5;
    [self.view addSubview:self.backGroudView];
    //姓名
    [self addNameView];
    //性别
    [self addSexView];
    //年龄
    [self addAgeView];
    //主诉
    [self addChiefComplaintView];
    //现病史
    [self addPresentHistoryView];
    //医院
    [self addHospitalView];
    //医生
    [self addDoctorView];
    //时间
    [self addDateView];
}
//姓名
- (void)addNameView
{
    /*  个人病例  */
    UILabel *caseLabel = [[UILabel alloc] init];
    caseLabel.text = @"个人病例";
    caseLabel.font = [UIFont systemFontOfSize:15];
    caseLabel.textAlignment = NSTextAlignmentLeft;
    [self.backGroudView addSubview:caseLabel];
    [caseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.025);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kLabelHeight));
    }];
    
    UIView *nameView = [[UIView alloc] init];
    nameView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:nameView];
    [nameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.025 + kLabelHeight);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kViewHeight));
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"姓名";
    label.textAlignment = NSTextAlignmentLeft;
    [nameView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.3, kLabelHeight));
    }];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textAlignment = NSTextAlignmentRight;
    self.nameLabel.text = self.medical.patient.name;
    [nameView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.right.offset(-(kWidth * 0.1));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kLabelHeight));
    }];
}
//性别
- (void)addSexView
{
    /*  性别  */
    UIView *sexView = [[UIView alloc] init];
    sexView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:sexView];
    [sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.025 + kLabelHeight + kViewHeight + 1);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kViewHeight));
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"性别";
    label.textAlignment = NSTextAlignmentLeft;
    [sexView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.3, kLabelHeight));
    }];
    
    self.sexLabel = [[UILabel alloc] init];
    self.sexLabel.textAlignment = NSTextAlignmentRight;
    self.sexLabel.text = self.medical.patient.sex;
    [sexView addSubview:self.sexLabel];
    [self.sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.right.offset(-(kWidth * 0.1));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kLabelHeight));
    }];
}
//年龄
- (void)addAgeView
{
    /*  年龄  */
    UIView *ageView = [[UIView alloc] init];
    ageView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:ageView];
    [ageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.025 + kLabelHeight + 2 * kViewHeight + 2);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kViewHeight));
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"年龄";
    label.textAlignment = NSTextAlignmentLeft;
    [ageView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.3, kLabelHeight));
    }];
    
    self.ageLabel = [[UILabel alloc] init];
    self.ageLabel.textAlignment = NSTextAlignmentRight;
    self.ageLabel.text = [self getAge];
    [ageView addSubview:self.ageLabel];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.right.offset(-(kWidth * 0.1));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kLabelHeight));
    }];

}
//主诉
- (void)addChiefComplaintView
{
    //主诉
    UILabel *chiefComplaintLabel = [[UILabel alloc] init];
    chiefComplaintLabel.text = @"主诉";
    chiefComplaintLabel.textAlignment = NSTextAlignmentLeft;
    chiefComplaintLabel.font = [UIFont systemFontOfSize:15];
    [self.backGroudView addSubview:chiefComplaintLabel];
    [chiefComplaintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.025 + kLabelHeight + 3 * kViewHeight + 2);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth, kLabelHeight));
    }];
    
    self.chiefComplaintView = [[UITextView alloc] init];
    //设置字体名字和字体大小
    self.chiefComplaintView.font = [UIFont fontWithName:@"Arial"size:16.0];
    self.chiefComplaintView.delegate = self;//设置它的委托方法
    self.chiefComplaintView.text = self.medical.chiefComplaint;
    self.chiefComplaintView.scrollEnabled = YES;//是否可以拖动
    self.chiefComplaintView.editable = NO;//禁止编辑
    self.chiefComplaintView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
    self.chiefComplaintView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:self.chiefComplaintView];
    [self.chiefComplaintView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.025 + 2 * kLabelHeight + 3 * kViewHeight + 2);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kHeight * 0.16));
    }];
}
//现病史
- (void)addPresentHistoryView
{
    //现病史
    UILabel *presentHistoryLabel = [[UILabel alloc] init];
    presentHistoryLabel.text = @"现病史";
    presentHistoryLabel.textAlignment = NSTextAlignmentLeft;
    presentHistoryLabel.font = [UIFont systemFontOfSize:15];
    [self.backGroudView addSubview:presentHistoryLabel];
    [presentHistoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.185 + 2 * kLabelHeight + 3 * kViewHeight + 2);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth, kLabelHeight));
    }];

    self.presentHistoryView = [[UITextView alloc] init];
    //设置字体名字和字体大小
    self.presentHistoryView.font = [UIFont fontWithName:@"Arial"size:16.0];
    self.presentHistoryView.delegate = self;//设置它的委托方法
    self.presentHistoryView.text = self.medical.presentHistory;
    self.presentHistoryView.scrollEnabled = YES;//是否可以拖动
    self.presentHistoryView.editable = NO;//禁止编辑
    self.presentHistoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
    self.presentHistoryView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:self.presentHistoryView];
    [self.presentHistoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.185 + 3 * kLabelHeight + 3 * kViewHeight + 2);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kHeight * 0.16));
    }];
}
//医院
- (void)addHospitalView
{
    /*   医院信息  */
    UILabel *hospitalLabel = [[UILabel alloc] init];
    hospitalLabel.text = @"医院信息";
    hospitalLabel.textAlignment = NSTextAlignmentLeft;
    hospitalLabel.font = [UIFont systemFontOfSize:15];
    [self.backGroudView addSubview:hospitalLabel];
    [hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.345 + 3 * kLabelHeight + 3 * kViewHeight + 2);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth, kLabelHeight));
    }];
    
    /* 医院 */
    UIView *hospitalView = [[UIView alloc] init];
    hospitalView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:hospitalView];
    [hospitalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.345 + 4 * kLabelHeight + 3 * kViewHeight + 2);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kViewHeight));
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"医院";
    label.textAlignment = NSTextAlignmentLeft;
    [hospitalView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.3, kLabelHeight));
    }];
    
    self.hospitalNameLabel = [[UILabel alloc] init];
    self.hospitalNameLabel.text = self.medical.hospitalName;
    self.hospitalNameLabel.textAlignment = NSTextAlignmentRight;
    [hospitalView addSubview:self.hospitalNameLabel];
    [self.hospitalNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.right.offset(-(kWidth * 0.1));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kLabelHeight));
    }];
}
//医生
- (void)addDoctorView
{
    /*  医生  */
    UIView *doctorView = [[UIView alloc] init];
    doctorView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:doctorView];
    [doctorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.345 + 4 * kLabelHeight + 4 * kViewHeight + 3);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kViewHeight));
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"医生";
    label.textAlignment = NSTextAlignmentLeft;
    [doctorView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.3, kLabelHeight));
    }];
    
    self.doctorNameLabel = [[UILabel alloc] init];
    self.doctorNameLabel.text = self.medical.doctorName;
    self.doctorNameLabel.textAlignment = NSTextAlignmentRight;
    [doctorView addSubview:self.doctorNameLabel];
    [self.doctorNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.right.offset(-(kWidth * 0.1));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kLabelHeight));
    }];
}
//时间
- (void)addDateView
{
    /*  时间  */
    UIView *dateView = [[UIView alloc] init];
    dateView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:dateView];
    [dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kHeight * 0.345 + 4 * kLabelHeight + 5 * kViewHeight + 4);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(kWidth, kViewHeight));
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"时间";
    label.textAlignment = NSTextAlignmentLeft;
    [dateView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(kWidth * 0.03);
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.3, kLabelHeight));
    }];
    
    self.medicalDateLabel = [[UILabel alloc] init];
    NSString *timeString = [self.medical.writeTime substringToIndex:16];
    self.medicalDateLabel.text = timeString;
    self.medicalDateLabel.textAlignment = NSTextAlignmentRight;
    [dateView addSubview:self.medicalDateLabel];
    [self.medicalDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.right.offset(-(kWidth * 0.1));
        make.size.mas_equalTo(CGSizeMake(kWidth * 0.5, kLabelHeight));
    }];
    
}

//计算年龄
- (NSString *)getAge
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:self.medical.patient.birthdate];

    NSTimeInterval dateDiff = [date timeIntervalSinceNow];
    int patientAge = trunc(dateDiff/(60*60*24))/365;
    return [NSString stringWithFormat:@"%d",(patientAge * -1)];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
